package com.bjc.core.controller;

import java.util.List;

import com.bjc.base.core.annotation.Permission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.bjc.base.bean.ResultVO;
import com.bjc.base.core.ResultGenerator;
import com.bjc.base.core.annotation.PermissionPath;
import com.bjc.base.validation.SelectTwo;
import com.bjc.core.bean.request.DimensionItemReqVO;
import com.bjc.core.bean.response.DimensionItemVO;
import com.bjc.core.service.IDimensionItemService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 维度数据范围控制器
 * 
 * @author 许自辉
 * @date 2017年7月21日
 */

@SpringBootConfiguration
@RestController
@RequestMapping("api/cloud")
@PermissionPath("系统管理/维度/数据范围")
@Api(value = "dimensionItem-api", tags = { "维度相关接口" })
public class DimensionItemController {

	@Autowired
	private IDimensionItemService dimensionItemService;

	@ApiOperation("分页查询维度数据范围")
	@Permission("dimensionItem:findPage")
	@GetMapping("dimensionItem/findPage")
	public ResultVO getDimensionItemPage(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize,
			DimensionItemReqVO dimensionItemReqVO) {
		PageHelper.startPage(pageNum, pageSize);
		List<DimensionItemVO> dimensionItemVOs = dimensionItemService.getList(dimensionItemReqVO);
		return ResultGenerator.getSuccess(new PageInfo<DimensionItemVO>(dimensionItemVOs));
	}

	@ApiOperation("分页查询用户角色未拥有的数据范围")
	@Permission("dimensionItem:userRole:findPage")
	@GetMapping("dimensionItem/userRole/findPage")
	public ResultVO getDimensionItemList(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize,
			@Validated({ SelectTwo.class }) DimensionItemReqVO dimensionItemReqVO) {
		PageHelper.startPage(pageNum, pageSize);
		List<DimensionItemVO> dimensionItemVOs = dimensionItemService.getNoRoleList(dimensionItemReqVO);
		return ResultGenerator.getSuccess(new PageInfo<DimensionItemVO>(dimensionItemVOs));
	}

	@ApiOperation("添加数据范围")
	@Permission("dimensionItem:create")
	@PostMapping("dimensionItem/create")
	public ResultVO createDimensionItem(@RequestBody @Validated DimensionItemReqVO dimensionItemReqVOs) {
		int count = dimensionItemService.insert(dimensionItemReqVOs);
		if (count == -1) {
			return ResultGenerator.getFail("该数据范围code已经存在！");
		}
		return ResultGenerator.getInsertSuccess(count);
	}

	@ApiOperation("更新数据范围")
	@Permission("dimensionItem:update")
	@PutMapping("dimensionItem/update")
	public ResultVO updateDimensionItem(@RequestBody @Validated DimensionItemReqVO dimensionItemReqVOs) {
		int count = dimensionItemService.update(dimensionItemReqVOs);
		if (count == -1) {
			return ResultGenerator.getFail("该数据范围code已经存在！");
		}
		return ResultGenerator.getUpdateSuccess(count);
	}

	@ApiOperation("删除数据范围")
	@Permission("dimensionItem:delete:id")
	@DeleteMapping("dimensionItem/delete/{id}")
	public ResultVO deleteDimensionItem(@ApiParam(value = "数据范围id", required = true) @PathVariable("id") int id) {
		int count = dimensionItemService.deleteById(id);
		return ResultGenerator.getDeleteSuccess(count);
	}
}
